/*
 * @Descripttion: 
 * @Author: junhuafly
 * @Date: 2020-03-09 15:52:14
 * @LastEditors: junhuafly
 * @Email: junhuafly@163.com
 * @LastEditTime: 2020-04-10 15:01:17
 */

import { orderAPI } from "../../../../api/index"

import {
  returnStatusToast,
} from '../../../../utils/index'
// pages/mine/evaluate/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fileList:[],
    orderDetailRates: [],
    form:{
      "logisticsRate": 0,
      "orderId": 0,
      "productRate": 0,
      "serviceRate": 0
    }
  },
  // 改变数据
  onChange({detail,currentTarget:{dataset:{type}}}){
    let temp = ''
    if(type.startsWith('comment')){
      temp = `orderDetailRates[${type.split('_').pop()}].comment`
    }else{
      temp = `form.${type}`
    }

    this.setData({
        [temp]:detail
      })
  },
  // 删除图片
  onDelImage({
    detail,currentTarget:{dataset:{index_pro}}
  }) {
    const {
      index
    } = detail
    const fileList = this.data.orderDetailRates[index_pro].images
    const temp = `orderDetailRates[${index_pro}].images`
    fileList.splice(index, 1)
    this.setData({
      [temp]: fileList,
    })
  },
  // 获取图片
  handleImage({detail:{file},currentTarget:{dataset:{index_pro}}}){
    const temp = `orderDetailRates[${index_pro}].images`
    this.setData({
      [temp]:[...(this.data.orderDetailRates[index_pro].images),...file]
    })
  },
  // 上传图片
  async uploadImage(wait_img){
     let images = []
     let files = []
     wait_img.forEach(item=>{
       files.push(item.path)
     })
     if(files.length){
       const _ = await UploadFile.upload(files, 'OTHER', (progress) => {})
       if (_ && _.length === files.length) {
         _.forEach((item, index) => {
           try {
             const res = JSON.parse(item)
             if (res.code === 200) {
               images.push(res.data)
             }
           } catch (error) {
             wx.showToast({
               title: '解析上传返回结果出错',
               icon: 'none',
               duration: 800,
             });
           } finally {
             wx.hideToast();
           }
         })
       }
     }
     return images
  },
  // 发布评论
  onPublish(){
    this.data.orderDetailRates.forEach( async item=>{
      item.images = await this.uploadImage(item.images)
    })
    this.setData({
      'form.orderDetailRates':this.data.orderDetailRates
    })

    // 发布评论
    orderAPI.rateGoods(this.data.form).then(async res=>{
      returnStatusToast(res,()=>{
        wx.redirectTo({
          url: '/pages/mine/order/afterOrder/status/index?status=1&message=评论成功&title=评论状态',
        })
      })
    })
    

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 获取订单列表
    const event = this.getOpenerEventChannel()
    event.on('params', ({info:infos})=>{
        infos.details.forEach(item=>{
          this.data.orderDetailRates.push(
            {
              "comment": "",
              "images": [],
              "orderDetailId": item.orderDetailId
            })
        })
        this.setData({
          infos,
          'form.orderId':infos.orderId,
          orderDetailRates:this.data.orderDetailRates
        })
      });


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})